package hash.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * 36. 有效的数独
 * @author haomin
 * @date 2022/03/16 21:14
 **/
public class Num36 {
    public boolean isValidSudoku(char[][] board) {
        Set<Character> rowSet = new HashSet<>();
        Set<Character> colSet = new HashSet<>();
        Set<Character> gridSet = new HashSet<>();
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                // 行
                if (board[j][i] != '.' && !rowSet.add(board[j][i]))
                    return false;
                // 列
                if (board[i][j] != '.' && !colSet.add(board[i][j]))
                    return false;
                // 九宫格校验
                if (board[i % 3 * 3 + j / 3][i / 3 * 3 + j % 3] != '.' && !gridSet.add(board[i % 3 * 3 + j / 3][i / 3 * 3 + j % 3]))
                    return false;

            }
            rowSet.clear();
            colSet.clear();
            gridSet.clear();
        }
        return true;
    }
}